package com.kmproject.haifu.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.ui.adapter.FragmentTitlesAdapter;
import com.kmproject.haifu.mvp.ui.fragment.FollowListFragment;
import com.kmproject.haifu.mvp.ui.widget.NoScrollViewPager;
import com.kmproject.haifu.mvp.ui.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 关注列表
 */
public class FollowListActivity extends BaseActivity implements IView {
    @BindView(R.id.toolbar_title)
    TextView toolbar_title;
    @BindView(R.id.follow_list_mi)
    MagicIndicator follow_list_mi;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    private String userId;
    private UserBean userBean;
    private List<String> titles = null;
    private FollowListFragment userFollowListFragment;
    private FollowListFragment storeFollowListFragment;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_follow_list;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        boolean isCurrentUser = false;
        userBean = DataHelper.getDeviceData(this, AppConstant.USER_INFO);//获取用户信息
        Intent intent = getIntent();
        if (intent != null) {
            userId = intent.getStringExtra("userId");
        }
        if (userBean != null && userBean.getUserId() != null) {
            if (userId.equals(userBean.getUserId())) {
                isCurrentUser = true;
                toolbar_title.setText(getString(R.string.follow_title_my));
            } else {
                toolbar_title.setText(getString(R.string.follow_title_ta));
            }
        } else {
            toolbar_title.setText(getString(R.string.follow_title_ta));
        }
        titles = Arrays.asList(getResources().getStringArray(R.array.follow_tab_title));
        List<Fragment> fragments = new ArrayList<>();
        userFollowListFragment = FollowListFragment.newInstance(0, userId, isCurrentUser);
        storeFollowListFragment = FollowListFragment.newInstance(1, userId, isCurrentUser);
        fragments.add(userFollowListFragment);
        fragments.add(storeFollowListFragment);
        viewpager.setAdapter(new FragmentTitlesAdapter(getSupportFragmentManager(), this, titles, fragments));
        viewpager.setCurrentItem(0);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));
//                simplePagerTitleView.setWidth((ScreenUtils.getScreenWidth() - 30) / 3);
                simplePagerTitleView.setWidth(ScreenUtils.getScreenWidth() / 5);
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_191919));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_191919));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.CENTER;
//                layoutParams.setMargins(0, 0, 0, 0);//4个参数按顺序分别是左上右下
                simplePagerTitleView.setLayoutParams(layoutParams);
                simplePagerTitleView.setOnClickListener(v -> {
                    follow_list_mi.onPageSelected(index);
                    commonNavigator.getAdapter().notifyDataSetChanged();
                    viewpager.setCurrentItem(index);

                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setLineHeight(11.0f);
                linePagerIndicator.setColors(ArmsUtils.getColor(FollowListActivity.this, R.color.color_F9D35A));

                return linePagerIndicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }

        });

        follow_list_mi.setNavigator(commonNavigator);
        ViewPagerHelper.bind(follow_list_mi, viewpager);
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}